﻿using FlyingEye.Middlewares.RateLimiting;
using System.Diagnostics.Metrics;
using System.Threading.RateLimiting;
using System.Timers;

namespace TestDemo
{

    public class Hello
    {

    }
    public class MeterFactory : IMeterFactory
    {
        public Meter Create(MeterOptions options)
        {
            return new Meter(options);
        }

        public void Dispose()
        {
        }
    }


    internal class Program
    {
        private static System.Timers.Timer? _timer;

        public static void Main()
        {
            _timer = new System.Timers.Timer(1000); // 每隔 1 秒触发一次 Elapsed 事件
            _timer.Elapsed += _timer_Elapsed;
            _timer.Enabled = true;
            _timer.Start();

            Console.WriteLine("按任意键停止计时器。");
            Console.ReadLine();
        }

        private static void _timer_Elapsed(object? sender, ElapsedEventArgs e)
        {
            Console.WriteLine("Timer 操作执行于 {0}", e.SignalTime);

        }
        

        //static async Task Main(string[] args)
        //{

        //    ConcurrentUpDownCounter concurrentUpDownCounter = new ConcurrentUpDownCounter();
        //    Console.WriteLine(concurrentUpDownCounter.Count);
        //    concurrentUpDownCounter.Increment();
        //    concurrentUpDownCounter.Increment();
        //    concurrentUpDownCounter.Increment();
        //    Console.WriteLine(concurrentUpDownCounter.Count);
        //    concurrentUpDownCounter.Decrement();
        //    Console.WriteLine(concurrentUpDownCounter.Count);

        //    Hello hello = new Hello();
        //    var slidingWindowRateLimiterOptions = new SlidingWindowRateLimiterOptions();
        //    slidingWindowRateLimiterOptions.PermitLimit = 4;
        //    slidingWindowRateLimiterOptions.Window = TimeSpan.FromSeconds(5);
        //    slidingWindowRateLimiterOptions.SegmentsPerWindow = 5;
        //    slidingWindowRateLimiterOptions.QueueLimit = 10;
        //    slidingWindowRateLimiterOptions.QueueProcessingOrder = QueueProcessingOrder.OldestFirst;
        //    slidingWindowRateLimiterOptions.AutoReplenishment = false;

        //    PartitionedRateLimiter<Hello>? partion = PartitionedRateLimiter.Create<Hello, string>(hello =>
        //    {
        //        RateLimitPartition<string> partition = RateLimitPartition.GetSlidingWindowLimiter("key", key => slidingWindowRateLimiterOptions);
        //        RateLimitPartition<string> partition2 = RateLimitPartition.GetSlidingWindowLimiter("key", key => slidingWindowRateLimiterOptions);


        //        //var limiter =  partition.Factory("key");
        //        //var limiter1 =  partition.Factory("key");

        //        //var code1 = limiter.GetHashCode();
        //        //var code2 = limiter1.GetHashCode();

        //        Console.WriteLine(partition.Factory("key").GetHashCode());
        //        return partition;
        //    });
        //    var factory = new MeterFactory();
        //    var meter = factory.Create("Microsoft");
        //    UpDownCounter<long> _activeRequestLeasesCounter = meter.CreateUpDownCounter<long>("1","1","1");

        //    _activeRequestLeasesCounter.Add(1);
        //    _activeRequestLeasesCounter.Add(-1);

        //    for (int i = 1; i < 500; i++)
        //    {
        //        // 这里返回的是 SlidingWindowLease 租聘类。
        //        RateLimitLease? lease = partion.AttemptAcquire(hello);

        //        // 判断是否被租聘
        //        if (lease.IsAcquired)
        //        {
        //            Console.WriteLine($"AttemptAcquire {i}");
        //        }
        //        else
        //        {
        //            Console.WriteLine("AcquireAsync start");
        //            // 阻塞等待可用的租聘
        //            var tempLease = await partion.AcquireAsync(hello);
        //            Console.WriteLine("AcquireAsync end");

        //            if (tempLease.IsAcquired)
        //            {
        //               Console.WriteLine($"AttemptAcquire {i}");
        //            }
        //        }
        //    }
        //}
    }
}
